package org.example.struct.array;

/**
 * @author liyishan
 * @date 2024/2/1 09:43
 * @apiNote
 */

public class Test00917ReverseOnlyLetters {

    public static void main(String[] args) {
        String s = "a-bC-dEf-ghIj";
        System.out.println(reverseOnlyLetters(s));
    }

    public static String reverseOnlyLetters(String s) {
        char[] arr = s.toCharArray();
        int first = 0, last = arr.length - 1;
        while (first < last) {
            while (first < last && !Character.isLetter(arr[first])) {
                first++;
            }
            while (first < last && !Character.isLetter(arr[last])) {
                last--;
            }
            swap(arr,first++,last--);
        }
        return new String(arr);
    }

    public static void swap(char[] arr, int i, int j){
        char temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
